package com.example.stgv;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

public class PinCode extends Activity {

	ArrayList<String> arr;
	GridView gv;
//	EditText input;
	TextView selection, input;
	int i, max = 9, min = 0;
	String tmp = "";
	Button btnCancel, btnOK;
	DBHelper db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pin_code);

//		input = (EditText) findViewById(R.id.txt_inputPIN);
//		selection = (TextView) findViewById(R.id.txt_selection);
		input = (TextView) findViewById(R.id.txt_output);

		GridView gridview = (GridView) findViewById(R.id.gridview1);

		ImageAdapter imageAdapter = new ImageAdapter(this);
		gridview.setAdapter(imageAdapter);

		gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				int temp = position;
				if (temp == 9) {
					input.setText(null);
				} else if (temp == 11) {
					String str = input.getText().toString().trim();
                    if (str.length() >= 1) {
                            str = str.substring(0, str.length() - 1);
                            input.setText(str);}
				} else if (temp == 10) {
					temp = -1;
					input.setText(input.getText().toString().trim()
							+ (temp + 1));
				} else {
					input.setText(input.getText().toString().trim()
							+ (temp + 1));
				}
			}
		});

		btnOK = (Button) findViewById(R.id.btn_ok);
		db = new DBHelper(PinCode.this);
		btnOK.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				try {
					String pincode = db.convertMD5(input.getText().toString().trim());
					Cursor cur = db
							.runAllDateRQ("select mapin from pin where mapin ='"
									+ pincode + "' ");
					if (cur.getCount() == 0) {
						Toast.makeText(PinCode.this, "Sai mã PIN.",
								Toast.LENGTH_SHORT).show();
						input.setText(null);
						cur.close();
					} else {
						Intent openMain = new Intent(PinCode.this,
								MainActivity.class);
						startActivity(openMain);
						finish();
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnCancel = (Button) findViewById(R.id.btn_cancel);
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new BasicAlertDialogFragment().show(getFragmentManager(),
						"Xac Nhan");
				;
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pin_code, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_changePin) {
			Intent openClass = new Intent(this, ChangePin.class);
			startActivity(openClass);
		}
		return super.onOptionsItemSelected(item);
	}

	public boolean checkdup(String itmp1) {
		// DUng de kiem tra xem so vua sinh ra co bi trung khong.
		for (String j : arr) {
			// Duyet mang de so sanh voi itmp1
			if (itmp1 == j) {
				return true;
			}
		}
		return false;
	}

	public class BasicAlertDialogFragment extends DialogFragment {
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			// Thiết lập nội dung cần thông báo
			builder.setMessage("Ban co muon thoat chuong trinh?")
					.setPositiveButton("Có",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									finish();
								}
							})
					.setNegativeButton("Không",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									dialog.cancel();
								}
							});

			return builder.create();
		}
	}

}
